package com.mh.controller;

import com.mh.dto.R;
import com.mh.dto.bo.UseCouponBo;
import com.mh.dto.vo.UseCouponRecordVo;
import com.mh.pojo.CouponInfos;
import com.mh.service.CouponService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * Date:2024/11/3
 * author:zmh
 * description: 优惠券相关接口控制器
 **/

@RestController
@RequestMapping("/couponInfos")
@CrossOrigin
@Slf4j
@RequiredArgsConstructor
public class CouponInfosController {

    private final CouponService  couponService;

    /**
     * 创建优惠券
     * @param couponInfos 优惠券信息
     * @return ·
     */
    @PostMapping("/createCoupon")
    public R<String> createCoupon(@RequestBody CouponInfos couponInfos) {
        return couponService.createCoupon(couponInfos);
    }

    /**
     * 获取商家所有优惠券信息
     * @param storeId 商家id
     * @return ·
     */
    @GetMapping("/getCouponInfosByStoreId/{storeId}")
    public R<List<CouponInfos>> getCouponInfosByStoreId(@PathVariable String storeId) {
        return couponService.getCouponInfosByStoreId(storeId);
    }

    /**
     * 用户领取优惠券
     * @param userId 用户ID
     * @param couponId 优惠券ID
     * @return ·
     */
    @GetMapping("/userGetCoupon/{userId}/{couponId}")
    public R<String> userGetCoupon(@PathVariable("userId") String userId, @PathVariable("couponId") String couponId) {
        return couponService.userGetCoupon(userId, couponId);
    }

    /**
     * 获取用户所有优惠券信息
     * @param userId 用户ID
     * @return ·
     */
    @GetMapping("/getCouponInfosByUserId/{userId}")
    R<List<CouponInfos>> getCouponInfosByUserId(@PathVariable String userId){
        return couponService.getCouponInfosByUserId(userId);
    }

    /**
     * 获取下单商品时用户可用优惠券信息
     * @param userId 用户ID
     * @param goodsId 商品ID
     * @return ·
     */
    @GetMapping("/getUseAbleCouponInfosByUserId/{userId}/{goodsId}")
    R<List<CouponInfos>> getUseAbleCouponInfosByUserId(@PathVariable("userId") String userId,@PathVariable("goodsId")  String goodsId){
        return couponService.getUseAbleCouponInfosByUserId(userId, goodsId);
    }

    /**
     * 用户使用优惠券
     * @param useCouponBo 用户使用优惠券BO
     * @return ·
     */
    @PutMapping("/useCoupon")
    R<String> useCoupon(@RequestBody UseCouponBo  useCouponBo){
        return couponService.useCoupon(useCouponBo.getUserId(), useCouponBo.getCouponId());
    }

    /**
     * 检查用户是否已领取该优惠券
     * @param useCouponBo 用户使用优惠券BO
     * @return ·
     */
    @PostMapping("/checkIsGet")
    R<Boolean> checkIsGetByUserIdAndCouponId(@RequestBody UseCouponBo useCouponBo){
        return couponService.checkIsGetByUserIdAndCouponId(useCouponBo.getUserId(), useCouponBo.getCouponId());
    }

    /**
     * 获取用户使用优惠券记录
     * @param userId 用户ID
     * @return ·
     */
    @GetMapping("/getUseCouponRecordByUserId/{userId}")
    R<List<UseCouponRecordVo>> getUseCouponRecordByUserId(@PathVariable String userId){
        return couponService.getUseCouponRecordByUserId(userId);
    }
}
